//
//  CZMainViewController.swift
//  微博01
//
//  Created by zhu on 15/11/23.
//  Copyright © 2015年 qqq. All rights reserved.
//

import UIKit

class CZMainViewController: UITabBarController {

//    撰写按钮点击事件
    func composeClick(){
        //__FUNCTION__打印方法名称
        print(__FUNCTION__)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        //设置自定义的tabBar
//        let newTabBar = CZTabbar()
//        
//        //KVC
//        setValue(newTabBar, forKey: "tabBar")
//        
//        //设置点击事件
//        newTabBar.composeButton.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchDragInside)
//        
        //同时设置图片和字体的颜色
        tabBar.tintColor = UIColor.orangeColor()

        //首页
        let homeVC = CZHomeViewController()
        addChlidViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        //消息
        let messageVC = CZMessageViewController()
        addChlidViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        // 占位控制器
        addChildViewController(UIViewController())
        
        //发现
        let discoverVC = CZDiscoverViewController()
        addChlidViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        //我
        let profileVC = CZProfileViewController()
        addChlidViewController(profileVC, title: "我", imageName: "tabbar_profile")
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        // 设置撰写按钮的frame
        let width = tabBar.bounds.size.width / 5
        let frame = CGRect(x: 2 * width - 5, y: 0, width: width + 10, height: tabBar.bounds.height)
        composeButton.frame = frame
    }
    
    private func addChlidViewController(controller: UIViewController,title: String, imageName: String)
    {
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        
        //设置高亮图片名称
        let highlightedImageName = imageName + "_highlighted"
        //让选择的图片使用原来的颜色，不要洗头帮我们渲染
        let highlightedImage = UIImage(named: highlightedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        addChildViewController(UINavigationController(rootViewController: controller))
        
    }
    // 懒加载
    /// 撰写按钮
    lazy var composeButton: UIButton = {
        let button = UIButton()
        
        // 设置参数
        // 设置按钮背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 设置按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        // 添加到tabbar里面
        self.tabBar.addSubview(button)
        
        // 添加点击事件
        button.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()

    
    
    
    

}
